# -*- coding: utf-8 -*-
import httplib, urllib
import csv

def manBuild(filename):
    testReader = csv.reader(open(filename))
#    fp = open("./test.log", "w+")
    age = 0
    
    for row in testReader:
        count = len(row)
#        print 'the number of column : %d' % count
        if row[1].strip() == '0-3':
            age = 0
        elif row[1].strip() == '4-6':
            age = 4
        elif row[1].strip() == '7-9':
            age = 7
        elif row[1].strip() == '10-12':
            age = 10
        elif row[1].strip() == '13-18':
            age = 13
        elif row[1].strip() == '19-24':
            age = 19            
        elif row[1].strip() == '2':
            age = 24
        elif row[1].strip() == '3':
            age = 36
        elif row[1].strip() == '4':
            age = 48
        elif row[1].strip() == '5':
            age = 60
        elif row[1].strip() == '6':
            age = 72
        elif row[1].strip() == '7':
            age = 84
        elif row[1].strip() == '8':
            age = 96
        elif row[1].strip() == '9':
            age = 108
        elif row[1].strip() == '10':
            age = 120
        elif row[1].strip() == '11':
            age = 132
        elif row[1].strip() == '12':
            age = 144
        elif row[1].strip() == '13':
            age = 156
        elif row[1].strip() == '14':
            age = 168
        elif row[1].strip() == '15':
            age = 180
        elif row[1].strip() == '16':
            age = 192
        elif row[1].strip() == '17':
            age = 204
        elif row[1].strip() == '18':
            age = 216
        elif row[1].strip() == '19':
            age = 228
        elif row[1].strip() == '20-21':
            age = 240
        elif row[1].strip() == '22-24':
            age = 264
        elif row[1].strip() == '25-29':
            age = 300
        elif row[1].strip() == '30-34':
            age = 360
        elif row[1].strip() == '35-39':
            age = 420
        elif row[1].strip() == '40-49':
            age = 480
        elif row[1].strip() == '50-59':
            age = 600
        elif row[1].strip() == '60':
            age = 720
        else:
            age = 0
            
        params = urllib.urlencode({'report_year' : '1997-01-01', 'parts' : row[0], 'age' : age, 'avg' : row[2], 'dev' : row[3], 'top_5' : row[4], 'top_10' : row[5], 'top_25' : row[6], 'top_50' : row[7], 'top_75' : row[8], 'top_90' : row[9], 'top_95' : row[10], 'gender' : 'F'})      
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        conn = httplib.HTTPConnection("127.0.0.1:8000")
        conn.request("POST", "/logging/statistics/build/", params, headers)
        response = conn.getresponse()
        print response.status, response.reason
        data = response.read()
        conn.close()    

        print data
         
if __name__ == "__main__":
    manBuild("D:\www\statistics\man_build.csv")
    manBuild("D:\www\statistics\women_build.csv")